import { defineStore } from 'pinia';

export const useDeviceStore = defineStore('device', {
  state: () => ({
    devices: [],
    currentDevice: null,
    error: null
  }),
  actions: {
    addDevice(device) {
      this.devices.push(device);
    },
    updateDevice(index, device) {
      this.devices[index] = device;
    },
    removeDevice(index) {
      this.devices.splice(index, 1);
    },
    setCurrentDevice(device) {
      try {
        if (!device) {
          throw new Error('设备信息不能为空');
        }
        this.currentDevice = device;
        localStorage.setItem('currentDevice', JSON.stringify(device));
        this.error = null;
      } catch (error) {
        this.error = error.message;
        throw error;
      }
    },
    clearError() {
      this.error = null;
    }
  },
});